package com.java.module.chat.service.impl;

import java.util.List;

import com.java.module.chat.domain.DisableWord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.common.utils.redis.RedisCache;
import com.java.module.chat.mapper.DisableWordMapper;
import com.java.module.chat.service.IDisableWordService;

/**
 * 聊天：禁用词Service业务层处理
 * 
 * @author Administrator
 * @date 2021-05-13
 */
@Service
public class DisableWordServiceImpl implements IDisableWordService {

    @Autowired
    private DisableWordMapper disableWordMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String CACHE_KEY = "chatDisableWords";

    /**
     * 查询聊天：禁用词
     * 
     * @param wid 聊天：禁用词ID
     * @return 聊天：禁用词
     */
    @Override
    public DisableWord selectDisableWordById(Long wid) {
        return disableWordMapper.selectDisableWordById(wid);
    }

    /**
     * 查询聊天：禁用词列表
     * 
     * @param disableWord 聊天：禁用词
     * @return 聊天：禁用词
     */
    @Override
    public List<DisableWord> selectDisableWordList(DisableWord disableWord) {
        return disableWordMapper.selectDisableWordList(disableWord);
    }

    /**
     * 查询单个聊天：禁用词
     * 
     * @param disableWord 聊天：禁用词
     * @return 聊天：禁用词
     */
    @Override
    public DisableWord selectDisableWord(DisableWord disableWord) {
        return disableWordMapper.selectDisableWord(disableWord);
    }

    /**
     * 新增聊天：禁用词
     * 
     * @param disableWord 聊天：禁用词
     * @return 结果
     */
    @Override
    public int insertDisableWord(DisableWord disableWord) {
        String[] split = disableWord.getWords().split("\\n");
        String words = "";
        for (String string : split) {
            words += "," + string;
        }
        words = words.substring(1);
        disableWord.setWords(words);
        int i = disableWordMapper.insertDisableWord(disableWord);
        if (i > 0) {
            this.cacheDisableWords();
        }
        return i;
    }

    /**
     * 修改聊天：禁用词
     * 
     * @param disableWord 聊天：禁用词
     * @return 结果
     */
    @Override
    public int updateDisableWord(DisableWord disableWord) {
        int i = disableWordMapper.updateDisableWord(disableWord);
        if (i > 0) {
            this.cacheDisableWords();
        }
        return i;
    }

    /**
     * 批量删除聊天：禁用词
     * 
     * @param ids 需要删除的聊天：禁用词ID
     * @return 结果
     */
    @Override
    public int deleteDisableWordByIds(Long[] ids) {
        int i = disableWordMapper.deleteDisableWordByIds(ids);
        if (i > 0) {
            this.cacheDisableWords();
        }
        return i;
    }

    /**
     * 删除聊天：禁用词信息
     * 
     * @param id 聊天：禁用词ID
     * @return 结果
     */
    @Override
    public int deleteDisableWordById(Long id) {
        int i = disableWordMapper.deleteDisableWordById(id);
        if (i > 0) {
            this.cacheDisableWords();
        }
        return i;
    }

    /**
     * 缓存所有禁用词
     * 
     * @return
     */
    public List<DisableWord> cacheDisableWords() {
        DisableWord disableWord = new DisableWord();
        disableWord.setDelFlag(0);
        List<DisableWord> list = disableWordMapper.selectDisableWordList(disableWord);
        if (list != null && list.size() > 0) {
            redisCache.setCacheList(CACHE_KEY, list);
        }
        return list;
    }

    /**
     * 获取缓存的所有禁用词
     * 
     * @return
     */
    public List<DisableWord> getDisableWords() {
        List<DisableWord> disableWords = redisCache.getCacheList(CACHE_KEY);
        if (disableWords == null || disableWords.size() == 0) {
            disableWords = cacheDisableWords();
        }
        return disableWords;
    }

    /**
     * 过滤禁用词
     * 
     * @param str
     * @return
     */
    @Override
    public String filterDisableWord(final String str) {
        String newStr = str;
        if (StringUtils.isNotBlank(str)) {
            final String splitSymbol = ",";
            final String reqlaceSymbol = "***";
            List<DisableWord> disableWords = this.getDisableWords();
            for (DisableWord disableWord : disableWords) {
                String words = disableWord.getWords();
                if (StringUtils.isNotBlank(words)) {
                    String[] wordArr = words.split(splitSymbol);
                    for (String wrod : wordArr) {
                        newStr = newStr.replace(wrod, reqlaceSymbol);
                    }
                }
            }
        }
        return newStr;
    }

    /**
     * 过滤禁用词
     * 
     * @param str
     * @return
     */
    @Override
    public String isDisableWord(final String str) {
        String newStr = str;
        final String splitSymbol = ",";
        List<DisableWord> disableWords = this.getDisableWords();
        for (DisableWord disableWord : disableWords) {
            String words = disableWord.getWords();
            if (StringUtils.isNotBlank(words)) {
                String[] wordArr = words.split(splitSymbol);
                for (String wrod : wordArr) {
                    if (newStr.contains(wrod)) {
                        return words;
                    }
                }
            }
        }
        return "";
    }

}